package main

import (
	"crypto/md5"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"time"

	"strings"

	"github.com/Unknwon/goconfig"
	"github.com/astaxie/beego/httplib"
	"github.com/howeyc/fsnotify"
)

type sysFileInfo struct {
	fName  string
	fSize  int64
	fMtime time.Time
	fPerm  os.FileMode
	fMd5   string
	fType  bool
}

var remoteurlv string

func main() {
	//******设置远程连接
	c, err := goconfig.LoadConfigFile("./conf.ini")
	if err != nil {
		log.Println("找不到配置文件")
		//c.SetValue("", "", "empty")
		// var c1 *goconfig.ConfigFile
		// goconfig.SaveConfigFile(c1, "./conf.ini")
	} else {
		//c.SetValue("App", "Url", "empty")
		geturlv, geturlerr := c.GetValue("App", "Url")
		if geturlerr == nil {
			remoteurlv = geturlv
			watcher, err := fsnotify.NewWatcher()
			if err != nil {
				log.Fatal(err)
			}

			done := make(chan bool)

			// Process events
			go func() {
				for {
					select {
					case ev := <-watcher.Event:
						log.Println("event:", ev)
						//log.Println(getFileInfo(ev.Name))
						if !ev.IsDelete() {
							clientSend(ev.Name)
						}
					case err := <-watcher.Error:
						log.Println("error:", err)
					}
				}
			}()

			//监听设置的目录
			getpathv, getpatherr := c.GetValue("App", "Path")
			if getpatherr == nil {
				pathvarr := strings.Split(getpathv, "|")
				//err = watcher.Watch("")
				//err1 := watcher.Watch("data")
				for _, v := range pathvarr {
					filepatherr := filepath.Walk("./"+v, func(path string, f os.FileInfo, err error) error {
						if f == nil {
							return err
						}
						if f.IsDir() {
							errtmp := watcher.Watch(path)
							println(path)
							if errtmp != nil {
								log.Fatal(errtmp)
							}
							return nil
						}

						return nil
					})
					if filepatherr != nil {
						fmt.Printf("filepath.Walk() returned %v\n", filepatherr)
					}
				}

				// Hang so program doesn't exit
				<-done

				/* ... do stuff ... */
				watcher.Close()
			} else {
				log.Println("请设置监听目录")
			}

		} else {
			log.Println("请设置远程链接目录")
		}

		//goconfig.SaveConfigFile(c, "./conf.ini")
	}
	//********

}

func clientSend(files string) {
	//fInfo := getFileInfo(files)
	//newName := fmt.Sprintf("%s", fInfo.fName)
	//b := httplib.Post("http://222.128.40.129:8088/upfile")
	log.Println("remoteurlv:", remoteurlv)
	b := httplib.Post(remoteurlv)
	//b.Param("username", "astaxie")
	//b.Param("password", "123456")
	fmt.Println("uploadname", files)
	b.PostFile("uploadname", files)
	_, err := b.String()
	if err != nil {
		fmt.Println("send ERROR", err)
	}
}

func getFileInfo(filename string) *sysFileInfo {
	fi, err := os.Lstat(filename)
	if err != nil {
		fmt.Println("info ERROR", err)
		return nil
	}
	fileHandle, err := os.Open(filename)
	if err != nil {
		fmt.Println("open ERROR", err)
		return nil
	}

	h := md5.New()
	_, err = io.Copy(h, fileHandle)
	fileInfo := &sysFileInfo{
		fName:  fi.Name(),
		fSize:  fi.Size(),
		fPerm:  fi.Mode().Perm(),
		fMtime: fi.ModTime(),
		fType:  fi.IsDir(),
		fMd5:   fmt.Sprintf("%x", h.Sum(nil)),
	}
	return fileInfo
}
